#include "dswlocaltranssession.h"

#include "libdswservercenter/dswfwdsession.h"

CDswlocaltranssession::CDswlocaltranssession()
{
}

CDswlocaltranssession::~CDswlocaltranssession()
{
	LOG4CPLUS_DEBUG(log_dsw, "Destroy localtrans session!");
}

CDswlocaltranssession* CDswlocaltranssession::createNew(LPST_ENV_T pEnv, struct event_base *base, int nfd, int nHeadSize, char* pKey)
{
	struct timeval tv = {10, 0};
	struct bufferevent *pBev = bufferevent_socket_new(base, nfd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
	bufferevent_set_timeouts(pBev, &tv, NULL);
	return new CDswlocaltranssession(pEnv, pBev, nHeadSize, pKey);
}

CDswlocaltranssession::CDswlocaltranssession(LPST_ENV_T pEnv, struct bufferevent * pBev, int nHeadSize, char* pKey)
:  CTcpcombase(pEnv, pBev, nHeadSize)
, m_bStart(false)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create localtrans session!");
}

bool CDswlocaltranssession::ProcessMessage(char* pMsg, int nLen, struct evbuffer *outbuf)
{
	if (!m_bStart)
		return ProcessHeader(pMsg);
	return TransData(pMsg, nLen);
}

bool CDswlocaltranssession::ProcessHeader(char* pHeader)
{
	memcpy(&m_nDest, pHeader, sizeof(ST_LOCAL_TRANS_T));
	m_bStart = true;
	return true;
}

bool CDswlocaltranssession::TransData(char* pData, int nLen)
{
	m_nDest.pFunc(&m_nDest, pData, nLen, m_nDest.pCaller);
	return true;
}


